package com.itheima.bigevent.controller;

import com.itheima.bigevent.pojo.Category;
import com.itheima.bigevent.pojo.Result;
import com.itheima.bigevent.service.CategoryService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     * @param category 新增的文章分类
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        if (categoryService.add(category) > 0) {
            return Result.success();
        }else {
            return Result.error("新增失败");
        }
    }

    /**
     * 获取当前已登录用户创建的所有文章分类
     * @return 当前用户创建的文章分类列表
     */
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> categoryList = categoryService.list();
        return Result.success(categoryList);
    }

    /**
     * 获取文章分类详情
     * @param id 文章分类ID
     * @return 文章分类详情
     */
    @GetMapping("/detail")
    public Result<Category> detail(@Min(value = 1, message = "id必须大于0") Integer id) {
        return Result.success(categoryService.detail(id));
    }

    /**
     * 更新文章分类
     * @param category 更新后的文章分类
     * @return 更新结果
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     * @param id 要删除的文章分类的ID
     * @return
     */
    @DeleteMapping
    public Result delete(@NotNull @Min(value = 1, message = "id必须大于0") Integer id) {
        if (categoryService.delete(id) > 0) {
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }
}
